<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/10/25
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\common\facade\Arr;
use app\common\tools\ExportExcel;
use think\App;
use think\Console;
use think\Db;
use think\facade\Session;

/**
 *
 * Class Test
 * @package app\index\controller
 * @author feraner<feraner@163.com>
 * @date 2019/10/25
 */
class Test extends UserBase
{
    public function __construct(App $app = null)
    {
        parent::__construct($app);
    }


    /**
     * 模拟微信登录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function mokeLogin()
    {
        $userInfo = Db::name('user')
            //feraner openid
            //->where('wechat_openid', 'omnYWwjLBq6373eA9lrDYAVnfl7s')
            //田松 openid
            ->where('wechat_openid', 'omnYWwrcC-MPU75oi1I1gc52w1rg')
            ->field('last_login_ip,login_ip,password', true)
            ->find() ?: [];

        session('user', $userInfo);
        session('userId', Arr::get($userInfo, 'id'));
        exit();
        $this->redirect(url());
    }



    public function clearSession()
    {
        session('user', null);
        session('userId', null);
        Console::call('xclean:session');
        Session::clear();
        $this->redirect('/');
    }




    /**
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function testExcel()
    {
        $userList = Db::name('user')
            ->select() ?: [];

        $exportdata = [
            'cellTitle'     => [
                [
                    'name'      => '会员名称',
                    'dataName'  => 'name',
                ],
                [
                    'name'      => '会员邮箱',
                    'dataName'  => 'email',
                ],
                [
                    'name'      => '订单量',
                    'dataName'  => 'total_order_num',
                ],
                [
                    'name'      => '注册时间',
                    'dataName'  => 'created_at',
                    'handle'    => 'handleDate'
                ]
            ],
            'list'          => $userList,
            'otherExportList'     => [
                [
                    'sheetIndex'    => 1,
                    'sheetTitle'    => 'test月度报表',
                    'list'  => $userList,
                    'cellTitle'     => [
                        [
                            'name'      => '会员名称',
                            'dataName'  => 'name',
                        ],
                        [
                            'name'      => '会员邮箱',
                            'dataName'  => 'email',
                        ],
                        [
                            'name'      => '订单量',
                            'dataName'  => 'total_order_num',
                        ],
                        [
                            'name'      => '注册时间',
                            'dataName'  => 'created_at',
                            'handle'    => 'handleDate'
                        ]
                    ],
                ]
            ]
        ];
        (new ExportExcel())->exportDataToXls(
            'test',
            $exportdata
        );
    }




}
